<?php

include_once('Commons.php');

print_r(getRegionTree(243));

exit;
require('checks.php');

$limit = 1;

	$rh = query('SELECT u.id,u.username FROM user u
				LEFT JOIN profile p2 ON p2.user=u.id AND p2.current=1
				LEFT JOIN profile p1 ON p1.user=u.id
				WHERE p1.creation>p2.creation AND p1.status IS NULL
				OR p2.creation IS NULL AND p1.status IS NULL
				GROUP BY u.id
				ORDER BY p1.id DESC LIMIT '.$limit.';');

	while ($row = mysql_fetch_assoc($rh)) {
		$uid = $row['id'];
		$username = $row['username'];

		$profile = getLatestProfile($uid);
		$pid = $profile['id'];
		$description = $profile['description'];
		$email = $profile['email'];
		$country = $profile['country'];

		echo "$uid - $pid - $username - $email - $country<br>";

		if (in_array(country_id_to_code($country), $bad_countries)) {
			echo "country banned, skipping<br>";
			continue;
		}

		if (level2_check($description)) {
			decline($uid, $pid, $username, $email);
			continue;
		}
		if (level3_check($description)) {
			decline($uid, $pid, $username, $email);
			continue;
		}
		if (find_multiphrases($description)) {
			decline($uid, $pid, $username, $email);
			continue;
		}
		approve($uid, $pid, $username, $email);
		echo "<hr>";
	}


function decline($uid, $pid, $username, $email) {
	query("UPDATE profile SET current=0 WHERE user='$uid';");
	query("UPDATE profile SET status=1 WHERE user='$uid';");
	query("UPDATE profile SET valid = NULL WHERE id='$pid';");

	$parameters = array($username);
	sendMail('autorefuse', $email, $username, $parameters);

}

function approve($uid, $pid, $username, $email) {
	query("UPDATE profile SET current=0 WHERE user='$uid';");
	query("UPDATE profile SET valid=1, current=1 WHERE id='$pid';");

	$parameters = array($username);
	sendMail('autovalidate', $email, $username, $parameters);

}

?>